------------------------------------------------------------------------------------
-- game/entities/wemix.lua
--
-- @module      wemix
-- @author      ztw
-- @release     v1.0.0 - 2025/1/8
-- @copyright   2024
-- @usage
-- @type wemix
-- local wemix = import('game/entities/wemix')
------------------------------------------------------------------------------------
---@class wemix
local wemix          = {
    -- 模块版本 (主版本.次版本.修订版本)
    VERSION = '1.0.0',
    -- 作者备注 (更新日期 - 更新内容简述)
    AUTHOR_NOTE = '2025/1/8 - Initial release',
    -- 模块名称
    MODULE_NAME = 'wemix模块',
    -- 只读模式
    READ_ONLY = false,
}

local this           = wemix
local decider        = decider
local trace          = trace
local local_player   = local_player
local main_ctx       = main_ctx
---@type api
local api            = import("api/api")
---@type common
local common         = import("game/func/common")
local WEMIX_PATH_REQ = common.WEMIX_PATH_REQ
---@type redis
local redis          = import("game/func/redis")
------------------------------------------------------------------------------------
-- [事件] 预载函数(重载脚本)
------------------------------------------------------------------------------------
function this.super_preload()

end

function wemix.update_wemix_data(owner)
    local date = os.date("%Y-%m-%d, %H:%M:%S")
    local name = owner and owner.name or local_player:name()
    local region = main_ctx:c_region_name()
    local server = main_ctx:c_server_name()
    local local_ip = main_ctx:get_local_ip()
    local account = main_ctx:c_account()
    local password = main_ctx:c_password()
    local updateData = {
        date = date,
        name = name,
        server = region .. "_" .. server,
        ip = local_ip,
        account = account,
        password = password,
    }
    redis.set_json_redis_by_path_and_data(WEMIX_PATH_REQ, updateData)
    -- 5分钟后销毁
    main_ctx:redis_set_expire(WEMIX_PATH_REQ, 5 * 60)
end

function wemix.get_wemix_data_by_redis()
    local needUpdate = false
    local isSame = false
    local checkConnect = false
    local data = redis.get_json_redis_by_path(WEMIX_PATH_REQ)
    local myAccount = main_ctx:c_account()
    local myIp = main_ctx:get_local_ip()
    if data and not table.is_empty(data) then
        local local_ip = data.ip
        local account = data.account
        local isConnect = data.isConnect
        if isConnect then
            checkConnect = true
        end
        if myAccount == account and myIp == local_ip then
            isSame = true
        end
    else
        -- 没有数据，需要更新
        needUpdate = true
    end
    return needUpdate, isSame, checkConnect
end

------------------------------------------------------------------------------------
-- [内部] 将对象转换为字符串
--
-- @local
-- @treturn      string                     模块名称
------------------------------------------------------------------------------------
function wemix.__tostring()
    return this.MODULE_NAME
end

------------------------------------------------------------------------------------
-- [内部] 防止动态修改(this.READ_ONLY值控制)
--
-- @local
-- @tparam       table     t                被修改的表
-- @tparam       any       k                要修改的键
-- @tparam       any       v                要修改的值
------------------------------------------------------------------------------------
function wemix.__newindex(t, k, v)
    if this.READ_ONLY then
        error('attempt to modify read-only table')
        return
    end
    rawset(t, k, v)
end

------------------------------------------------------------------------------------
-- [内部] 设置item的__index元方法指向自身
--
-- @local
------------------------------------------------------------------------------------
wemix.__index = wemix

------------------------------------------------------------------------------------
-- [构造] 创建一个新的实例
--
-- @local
-- @tparam       table     args             可选参数，用于初始化新实例
-- @treturn      table                      新创建的实例
------------------------------------------------------------------------------------
function wemix:new(args)
    local new = {}
    -- 预载函数(重载脚本时)
    if this.super_preload then
        this.super_preload()
    end
    -- 将args中的键值对复制到新实例中
    if args then
        for key, val in pairs(args) do
            new[key] = val
        end
    end
    -- 设置元表
    return setmetatable(new, wemix)
end

-------------------------------------------------------------------------------------
-- 返回实例对象
-------------------------------------------------------------------------------------
return wemix:new()
